package com.goods.controller.business;

import com.github.pagehelper.PageInfo;
import com.goods.business.service.SuppliesCategoryService;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.business.ProductCategoryVO;
import com.goods.common.vo.system.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Administrator
 * 物资类别
 */
@Api(tags = "业务模块-物资类别相关接口")
@RestController
@RequestMapping("/business/productCategory")
public class ProductCategoryController {

    @Autowired
    private SuppliesCategoryService suppliesCategoryService;

    // 查询物资类别
    @ApiOperation(value = "物资类别",notes = "显示物资列表树")
    @GetMapping("/categoryTree")
    public ResponseBean<PageVO<ProductCategoryTreeNodeVO>> findSupplierList(
            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize,
            ProductCategoryTreeNodeVO productCategoryTreeNodeVO){

        //获取物资列表

        PageVO<ProductCategoryTreeNodeVO> findSupplierList =suppliesCategoryService.findSupplierList(pageNum,pageSize,productCategoryTreeNodeVO);
        return ResponseBean.success(findSupplierList);
    }

    //添加时,选择父分类时 回显父分类

    @ApiOperation(value = "物资类别添加-选择父分类",notes = "回显父分类")
    @GetMapping("/getParentCategoryTree")
    public ResponseBean<List<ProductCategoryTreeNodeVO>> getParentCategoryTree(){
        List<ProductCategoryTreeNodeVO>  ProductCategoryTreeNodeVOList=suppliesCategoryService.getParentCategoryTree();

        return ResponseBean.success(ProductCategoryTreeNodeVOList);

    }

    //添加物资分类

    @ApiOperation(value = "物资类别添加-添加弹框提交",notes = "添加分类")
    @PostMapping("/add")
    public ResponseBean add(@RequestBody ProductCategoryTreeNodeVO productCategoryTreeNodeVO){
        Boolean isAdd=suppliesCategoryService.add(productCategoryTreeNodeVO);

        if (isAdd){
            return ResponseBean.success("添加成功");
        }
        return ResponseBean.error("添加失败");

    }

    //删除物资分类

    @ApiOperation(value = "物资类别删除",notes = "删除分类")
    @DeleteMapping("/delete/{id}")
    public ResponseBean delete(@PathVariable Integer id){


        Boolean isDelete=suppliesCategoryService.delete(id);
        if (isDelete){
            return ResponseBean.success("删除成功");
        }
        return ResponseBean.error("删除失败,有子类无法删除");
    }

    //编辑物资分类之回显

    @ApiOperation(value = "物资类别编辑回显",notes = "编辑分类根据Id回显")
    @GetMapping("/edit/{id}")
    public  ResponseBean<ProductCategoryVO> edit(@PathVariable Integer id){
        ProductCategoryVO productCategoryVO=suppliesCategoryService.edit(id);

        return ResponseBean.success(productCategoryVO);

    }
    //编辑物资分类之保存
    @ApiOperation(value = "物资类别编辑保存",notes = "编辑分类根据Id更新")
    @PutMapping("/update/{id}")
    public ResponseBean update(@RequestBody ProductCategoryVO productCategoryVO,
                                                  @PathVariable Integer id){
        Boolean isUpdated=suppliesCategoryService.update(productCategoryVO,id);
        if (isUpdated){
            return ResponseBean.success("更新成功");
        }
        return ResponseBean.error("更新失败");
    }


}
